<?php
/**
 * @package App
 * @subpackage resource
 */
 
/**
 *
 * お知らせリソース
 * 
 * @package     App
 * @subpackage  resource
 * @author      $Author: moriwaki $
 * @version     $Id: Information.php 360 2008-09-29 01:58:40Z moriwaki $
 * 
 */
class Information extends BEAR_Vo
{
 
    /**
     * テーブル名
     */
    private $_table = "information";
 
    /**
     * 作成
     * 
     * @param array $values
     * + name: string 名前
     * @return integer id or error
     */
    public function onCreate($values)
    {
        //assert & mapping
        self::assert(is_string($values['subject']));
        self::assert(is_string($values['body']));
        self::assert(is_string($values['public_time']));
        //db
        $app_db = App_DB::getInstance();
        $extended = & $app_db->extended;
        /* @var $extended MDB2_Extended */
        $values['insert_time'] = _BEAR_DATETIME; //現在時刻
        $values['enabled'] = 1;
        $result = $extended->autoExecute($this->_table, $values, MDB2_AUTOQUERY_INSERT);
        return $result;
    }
 
    /**
     * 変更
     * 
     * @param array $values
     * + id: integer $id
     * + name: string 名前
     * @return integer id or error
     */
    public function onUpdate($values)
    {
        //assert & mapping
        self::assert(is_string($values['subject']));
        self::assert(is_string($values['body']));
        self::assert(is_string($values['public_time']));
        //db
        $db = App_DB::getInstance();
        $extended = & $db->extended;
        $values['update_time'] = _BEAR_DATETIME;
        /* @var $extended MDB2_Extended */
        $where = 'id = ' . $db->quote($values['id'], 'integer');
        $result = $extended->autoExecute($this->_table, $values, MDB2_AUTOQUERY_UPDATE, $where);
        return $result;
    }
 
    /**
     * 読み込み
     * 
     * @param array $values
     * + mode: strign $mode
     * @return array
     */
    public function onRead($values)
    {
        //assert & mapping
        self::assert(isset($values['mode']));
        $mode = $values['mode'];
        $limit = App_Values::getValue('limit', $values, 10);
        $db = App_DB::getInstance();
        switch($mode){
        case 'id' :
            $where = ' WHERE id = ' . $db->quote($values['id'], 'integer');
            $where .= ' and enabled = 1';
            $getResultMode = 'one';
            break;
        case 'list':
            $where = ' where enabled = 1';
            $getResultMode = 'pager';
            break;
        }

        $sql = "SELECT * FROM {$this->_table}{$where}";

        $pagerOptions = array();
        $pagerOptions['perPage'] = $limit;
        $result = App_DB::getQueryResult($sql, $pagerOptions, $getResultMode);

        return $result;
    }
 
    /**
     * 削除
     * 
     * @param array $values
     * + id: integer id
     * @return MDB2_Result
     */
    public function onDelete($values)
    {
        self::assert(isset($values['id']));
        $db = App_DB::getInstance();
        $extended = & $db->extended;
        $values['update_time'] = _BEAR_DATETIME;
        $values['enabled'] = false;
        /* @var $extended MDB2_Extended */
        $where = 'id = ' . $db->quote($values['id'], 'integer');
        $result = $extended->autoExecute($this->_table, $values, MDB2_AUTOQUERY_UPDATE, $where);
        return $result;
    }
}
